// 优化显示的时间
function you_hua_time(data) {
  let str = data.toString()
  return str[1] ? str : '0' + str
}

// 获取数字数组
function get_num_arr(start, end, str = "") {
  start = start - 0;
  end = end - 0;
  let array = [];
  for (let i = start; i <= end; i++) {
    array.push(you_hua_time(i) + str);
  }
  return array;
}

// 获取今天的年月日时分秒
function now_time() {
  // 获取时间
  let now = new Date();
  // 时间戳
  let time = now.getTime();
  // 获取年
  let nian = now.getFullYear();
  // 获取月
  let yue = now.getMonth() + 1;
  // 获取日
  let ri = now.getDate();
  // 获取时
  let shi = now.getHours();
  // 获取分
  let fen = now.getMinutes();
  // 获取秒
  let miao = now.getSeconds();

  // 返回年月日时分秒
  return {
    time: time,
    nian: you_hua_time(nian),
    yue: you_hua_time(yue),
    ri: you_hua_time(ri),
    shi: you_hua_time(shi),
    fen: you_hua_time(fen),
    miao: you_hua_time(miao),
    ri_qi: [nian, yue, ri].map(you_hua_time).join('-') + ' ' + [shi, fen, miao].map(you_hua_time).join(':')
  };
}

// 获取今后的年
function nian(nian, length) {
  let arr = [];
  for (let i = 0; i < length; i++) {
    arr.push(nian - 0 + i);
  }
  return arr;
}

// 获取该年的月
function yue(nian) {
  let new_time = now_time();
  let arr = [];
  // 如果是今年,获取今月之后的月份（包括今月）
  if (nian == new_time.nian) {
    arr = get_num_arr(new_time.yue, 12);
  } else { // 如果不是今年,获取12个月
    arr = get_num_arr(1, 12);
  }
  return arr;
}

// 获取该年该月的日
function ri(nian, yue) {
  // 判断今年是否为闰年
  let flag = nian % 400 == 0 || (nian % 4 == 0 && nian % 100 != 0),
    arr = null;
  switch (yue) {
    case '01':
    case '03':
    case '05':
    case '07':
    case '08':
    case '10':
    case '12':
      arr = get_num_arr(1, 31)
      break;
    case '04':
    case '06':
    case '09':
    case '11':
      arr = get_num_arr(1, 30)
      break;
    case '02':
      arr = flag ? get_num_arr(1, 29) : get_num_arr(1, 28)
      break;
    default:
      arr = '月份格式不正确，请重新输入！'
  }
  // 如果是当前的年月，日要从今天开始
  let new_time = now_time();
  if (new_time.nian == nian && new_time.yue == yue) {
    let day = new_time.ri;
    arr.splice(0, arr.indexOf(day));
  }
  return arr;
}


// 倒计时
function d_j_s(time_str, success, time = 0) {
  let time_arr = time_str.split(":");

  // 将时分秒转成数字类型
  let shi = time_arr[0] - 0;
  let fen = time_arr[1] - 0;
  let miao = time_arr[2] - 0;
  // 延迟计时器,延迟时间为time，time为0时不延迟
  setTimeout(() => {
    miao -= 1;
    if (miao < 0) {
      miao = 59;
      fen -= 1;
      if (fen < 0) {
        fen = 59;
        shi -= 1;
        if (shi < 0) {
          return false;
        }
      }
    }

    // 显示优化
    if (shi < 10) {
      shi = '0' + shi;
    }
    if (fen < 10) {
      fen = '0' + fen;
    }
    if (miao < 10) {
      miao = '0' + miao;
    }
    // 获取新的时间
    let new_time = shi + ":" + fen + ":" + miao + "";
    success(new_time)
    // 继续调用函数
    d_j_s(new_time, success, 1000);
  }, time);
}

// 根据结束时间和当前时间得到倒计时
function countdown(end_time) {
  let _t = end_time * 1000 - now_time().time;
  let ri = 0,
    shi = 0,
    fen = 0,
    miao = 0,
    dao_ji_shi = 0;
  if (_t > 0) {
    ri = Math.floor(_t / 1000 / 60 / 60 / 24) < 10 ? '0' + Math.floor(_t / 1000 / 60 / 60 / 24).toString() : Math.floor(_t / 1000 / 60 / 60 / 24).toString();
    shi = Math.floor(_t / 1000 / 60 / 60 % 24) < 10 ? '0' + Math.floor(_t / 1000 / 60 / 60 % 24).toString() : Math.floor(_t / 1000 / 60 / 60 % 24).toString();
    shi = shi - 0 + ri * 24;
    fen = Math.floor(_t / 1000 / 60 % 60) < 10 ? '0' + Math.floor(_t / 1000 / 60 % 60).toString() : Math.floor(_t / 1000 / 60 % 60).toString();
    miao = Math.floor(_t / 1000 % 60) < 10 ? '0' + Math.floor(_t / 1000 % 60).toString() : Math.floor(_t / 1000 % 60).toString();
  }
  dao_ji_shi = shi + ":" + fen + ":" + miao;

  return dao_ji_shi;
}

// 获取倒计时
function get_djs(djs_time) {
  let _t = djs_time;
  let ri = 0,
    shi = 0,
    fen = 0,
    miao = 0,
    time = "";

  if (_t > 0) {
    ri = Math.floor(_t / 1000 / 60 / 60 / 24);
    shi = Math.floor(_t / 1000 / 60 / 60 % 24);
    fen = Math.floor(_t / 1000 / 60 % 60);
    miao = Math.floor(_t / 1000 % 60);

    shi = shi - 0 + ri * 24;

    ri = ri < 10 ? "0" + ri : ri;
    shi = shi < 10 ? "0" + shi : shi;
    fen = fen < 10 ? "0" + fen : fen;
    miao = miao < 10 ? "0" + miao : miao;
    time = shi + ":" + fen + ":" + miao;
  } else {
    time = "00:00:00";
  }
  return time;
}

module.exports = {
  // 获取今天的年月日时分秒
  now_time: now_time,
  // 获取今后的年
  nian: nian,
  // 获取该年的月
  yue: yue,
  // 获取该年该月的日
  ri: ri,
  // 倒计时
  d_j_s: d_j_s,
  // 根据结束时间和当前时间得到倒计时
  countdown: countdown,
  // 获取倒计时
  get_djs: get_djs,
}